<template>
  <div>
    <!--卡片-->
    <el-card class="box-card">
      <!--定义面包屑-->
      <el-breadcrumb separator=">">
        <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>客户管理</el-breadcrumb-item>
        <el-breadcrumb-item>会员等级</el-breadcrumb-item>
      </el-breadcrumb>
      <el-divider></el-divider>
      <!--搜索框  表单 :model="双向绑定表单的数据"-->
      <el-form :inline="true" :model="levelSelect"  class="demo-form-inline">
        <el-form-item >
          <el-input v-model="levelSelect.id" placeholder="请输入等级ID"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="levelSelect.name" placeholder="等级名字"></el-input>
        </el-form-item>
        <el-form-item >
          <el-input v-model="levelSelect.note" placeholder="请输入等级详情"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="initUserTable">查询</el-button>
          <el-button type="primary" @click="add()" >添加</el-button>
        </el-form-item>
      </el-form>

      <!--数据表格-->
      <template>
        <el-table
            :data="tableData"
            :border="true"
            stripe
            style="width: 100%">
          <!--            height="666"-->
          <el-table-column
              prop="id"
              label="等级id"
              width="95px"
              sortable
          >
          </el-table-column>
          <el-table-column
              prop="name"
              label="等级名"
              width="95px"
              sortable
          >
          </el-table-column>
          <el-table-column
              prop="growthPoint"
              label="成长值"
              sortable>
          </el-table-column>
          <el-table-column
              prop="defaultStatus"
              label="默认等级"
              sortable>
            <template slot-scope="scope">
              <span v-if="scope.row.defaultStatus===0">×</span>
              <span v-if="scope.row.defaultStatus===1">√</span>
            </template>
          </el-table-column>
          <el-table-column
              prop="freeFreightPoint"
              label="免运费标准"
              sortable>
          </el-table-column>
          <el-table-column
              prop="priviledgeComment"
              label="评论获奖励特权"
              sortable
              width="152px">
            <template slot-scope="scope">
              <span v-if="scope.row.priviledgeComment===0">×</span>
              <span v-if="scope.row.priviledgeComment===1">√</span>
            </template>
          </el-table-column>
          <el-table-column
              prop="priviledgePromotion"
              label="专享活动特权"
              sortable>
            <template slot-scope="scope">
              <span v-if="scope.row.priviledgePromotion===0">×</span>
              <span v-if="scope.row.priviledgePromotion===1">√</span>
            </template>
          </el-table-column>
          <el-table-column
              prop="priviledgeMemberPrice"
              label="会员价格特权"
              sortable>
            <template slot-scope="scope">
              <span v-if="scope.row.priviledgeMemberPrice===0">×</span>
              <span v-if="scope.row.priviledgeMemberPrice===1">√</span>
            </template>
          </el-table-column>
          <el-table-column
              prop="priviledgeBirthday"
              label="生日特权"
              sortable>
            <template slot-scope="scope">
              <span v-if="scope.row.priviledgeBirthday===0">×</span>
              <span v-if="scope.row.priviledgeBirthday===1">√</span>
            </template>
          </el-table-column>
          <el-table-column
              prop="note"
              label="等级详情"
              sortable>
          </el-table-column>
          <el-table-column
              prop="ratio"
              label="比例"
              sortable>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="180px">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" circle @click="updateClient(scope.row)"></el-button>
              <el-button type="danger" icon="el-icon-delete"  circle  @click="open(scope.row.id)"></el-button>
              <!--              <el-button type="warning" icon="el-icon-star-off" circle @click="showUserRole(scope.row.userId)"></el-button>-->
            </template>
          </el-table-column>
        </el-table>

        <!--分页-->
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
      </template>
    </el-card>
<!--  添加和删除的  表单-->
    <el-dialog
        :title="titleName"
        :visible.sync="fenRoleDialog"
        width="30%"
    >
      <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef" label-width="100px" class="demo-ruleForm">
        <el-form-item label="等级id" prop="id" style="display: none">
          <el-input v-model="ruleForm.id" ></el-input>
        </el-form-item>
        <el-form-item label="等级名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="成长值" prop="growthPoint">
          <el-input v-model="ruleForm.growthPoint"></el-input>
        </el-form-item>
        <el-form-item label="是否为默认等级" prop="defaultStatus">
          <el-radio-group v-model="ruleForm.defaultStatus">
            <el-radio :label="0" >×</el-radio>
            <el-radio :label="1" >√</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="免运费标准" prop="freeFreightPoint">
          <el-input v-model="ruleForm.freeFreightPoint"></el-input>
        </el-form-item>
        <el-form-item label="是否有评论获奖励特权" prop="priviledgeComment">
          <el-radio-group v-model="ruleForm.priviledgeComment">
            <el-radio :label="0" >×</el-radio>
            <el-radio :label="1" >√</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有专享活动特权" prop="priviledgePromotion">
          <el-radio-group v-model="ruleForm.priviledgePromotion">
            <el-radio :label="0" >×</el-radio>
            <el-radio :label="1" >√</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有会员价格特权" prop="priviledgeMemberPrice">
          <el-radio-group v-model="ruleForm.priviledgeMemberPrice">
            <el-radio :label="0" >×</el-radio>
            <el-radio :label="1" >√</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否有生日特权" prop="priviledgeBirthday">
          <el-radio-group v-model="ruleForm.priviledgeBirthday">
            <el-radio :label="0" >×</el-radio>
            <el-radio :label="1" >√</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="等级详情" prop="note">
          <el-input v-model="ruleForm.note"></el-input>
        </el-form-item>
        <el-form-item label="比例" prop="ratio">
          <el-input v-model="ruleForm.ratio"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveOrUpdate()">提交</el-button>
          <el-button @click="resetForm()">重置</el-button>
          <el-button @click="fenRoleDialog =false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Level",
  data(){
    return {
      //条件搜索对象
      levelSelect:{},
      //表格数据
      tableData:[],
      //每页显示的条数
      pageSize: 5,
      //当前的显示的页码
      currentPage:1,
      //总条数
      total:0,
      //分配角色框的显示
      fenRoleDialog:false,
      //表单元素
      ruleForm:{},
      //表单校验规则
      rules: {
        name: [
          { required: true, message: '请输入等级名', trigger: 'blur' },
        ],
      },
      //会员等级数据
      titleName:"",
    }
  },
  //加载玩调用
  created() {
    this.initUserTable();
  },

  methods:{
    handleClose(){
    },
    //清空表单
    resetForm() {
      this.$nextTick(function(){
        this.$refs.ruleFormRef.resetFields();
      })
    },
    //1.初始化列表
    initUserTable(){
      var that= this;
      this.$http.post(`/members/level/get/${this.currentPage}/${this.pageSize}`,this.levelSelect).then(function (r) {
        that.tableData=r.data.result.records;
        that.total=r.data.result.total;
      })
    },
    // 点击添加
    add(){
      this.titleName="添加";
      this.fenRoleDialog=true;
      //清空表单 不要使用 this.resetForm()会出现页面刚加载时点击添加，表单输入数据不显示的bug
      this.ruleForm={}
    },
    //点击修改弹出修改的弹出窗
    updateClient(data){
      this.titleName="修改"
      this.fenRoleDialog=true;
      this.ruleForm=JSON.parse(JSON.stringify(data))
    },
    //确认提交
    saveOrUpdate(){
      let that =this;
      this.$http.post("/members/level/saveOrUpdate",this.ruleForm).then(function (r) {
        that.fenRoleDialog=false;
        that.$message.success(r.data.message)
        that.initUserTable();
      })
    },
    //当改变没有显示的条数时触发该方法
    handleSizeChange(y){
      this.pageSize=y;
      this.initUserTable();
    },
    //切换页码时触发该方法
    handleCurrentChange(y){
      this.currentPage=y;
      this.initUserTable();
    },
    // 点击删除弹出确认窗口
    open(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).then(() => {
        this.deleteById(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //删除的方法
    deleteById(id){
      let that=this
      this.$http.delete(`/members/level/delect/${id}`).then(function (r){
        that.$message.success(r.data.message)
        that.initUserTable();
      })
    },

  }
}
</script>

<style scoped>

</style>
